﻿using System;
using System.Globalization;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Xml;
using System.Reflection;
using JigLibX.Physics;
using JigLibX.Collision;
using Services;
using Utilities.Menu.UIElements;
using BowMaster.Resources;
using Services.Menu;
using Utilities;

namespace BowMaster.Menu
{
    class CreditsMenu : DrawableGameComponent
    {
        //MyButton title;
        ArrayList<MyButton> rows;
        //MyButton email;
        MyButton menu;
        Texture2D emptyTexture, credits_bg, whiteTexture;
        SpriteFont arialHuge, titles, names, arial18, arial14, arial16;
        InputManager input_manager;
        SpriteBatch spriteBatch;
        StateManager StateManager;
        bool transition_complete;
        public CreditsMenu(Game game)
            : base(game)
        {
        }
        public override void Initialize()
        {
            spriteBatch = Game.Services.GetService(typeof(SpriteBatch)) as SpriteBatch;
            StateManager = Game.Services.GetService(typeof(AStateManager)) as StateManager;
            Game.Services.RemoveService(typeof(IInputManager));
            input_manager = new InputManager(Game);
            Game.Components.Add(input_manager);
            input_manager.OnTap += on_tap;
            base.Initialize();
        }
        protected override void LoadContent()
        {
            arialHuge = Game.Content.Load<SpriteFont>("Fonts/Arial54Ptx");

            titles = Game.Content.Load<SpriteFont>("Fonts/Arial26Ptx");
            arial16 = Game.Content.Load<SpriteFont>("Fonts/Arial16Ptx");
            names = Game.Content.Load<SpriteFont>("Fonts/Arial20Ptx");
            arial18 = Game.Content.Load<SpriteFont>("Fonts/Arial18Ptx");
            arial14 = Game.Content.Load<SpriteFont>("Fonts/Arial14Ptx");
            emptyTexture = Game.Content.Load<Texture2D>("Textures/emptyPixel");
            credits_bg = Game.Content.Load<Texture2D>("Textures/credits_bg");
            whiteTexture = Game.Content.Load<Texture2D>("Textures/whitePixel");
            var color = new Color (8,141,217);
            rows = new ArrayList<MyButton>();
            //rows.Add(new MyButton(new Rectangle(0, 800, 480, 100), Strings.Menu_Title, Color.Gold, arialHuge, Color.White, emptyTexture));
            //rows.Add(new MyButton(new Rectangle(0, 150 + 800, 480, 100), Strings.Authors, Color.White, arial18, Color.White, emptyTexture));
            //rows.Add(new MyButton(new Rectangle(0, 200 + 800, 480, 100), "Francesco Boscariol", Color.Green, arial18, Color.White, emptyTexture));
            //rows.Add(new MyButton(new Rectangle(0, 250 + 800, 480, 100), "Giuseppe Maggiore", Color.Blue, arial18, Color.White, emptyTexture));
            //rows.Add(new MyButton(new Rectangle(0, 300 + 800, 480, 100), "Andrea Gasparetto", Color.DarkSalmon, arial18, Color.White, emptyTexture));
            //rows.Add(new MyButton(new Rectangle(0, 400 + 800, 480, 100), "Beta Testers:", Color.LightSkyBlue, arial18, Color.White, emptyTexture));
            //rows.Add(new MyButton(new Rectangle(0, 450 + 800, 480, 100), "Chiara Boscariol", Color.Coral, arial18, Color.White, emptyTexture));
            //rows.Add(new MyButton(new Rectangle(0, 500 + 800, 480, 100), Strings.EMail, Color.Red, arial14, Color.White, emptyTexture));
            //rows.Add(new MyButton(new Rectangle(0, 580 + 800, 480, 100), Strings.ThanksForPlaying, Color.Red, arial18, Color.Gold, emptyTexture));
            //rows.Add(new MyButton(new Rectangle(0, 630 + 800, 480, 100), Strings.Version + (Game.IsTrial()?" - " + Strings.TrialVersion:""), Color.Green, arial14, Color.Gold, emptyTexture));

            rows.Add(new MyButton(new Rectangle(0, 0, 480, 184), Strings.Menu_Title, color, arialHuge));
            rows.Add(new MyButton(new Rectangle(0, 200, 480, 45), Strings.Developers, color, titles));
            rows.Add(new MyButton(new Rectangle(0, 250, 480, 30), "Francesco Boscariol", color, names));
            rows.Add(new MyButton(new Rectangle(0, 280, 480, 30), "Luca Cosmo", color, names));
            //rows.Add(new MyButton(new Rectangle(0, 310, 480, 30), "Giuseppe Maggiore", color, names));
            rows.Add(new MyButton(new Rectangle(50, 345, 190, 45), Strings.Graphics, color, arial18));
            rows.Add(new MyButton(new Rectangle(50, 390, 190, 30), "Luca Milan", color, arial18));
            rows.Add(new MyButton(new Rectangle(240, 345, 190, 45), Strings.SpecialThanks, color, arial18));
            rows.Add(new MyButton(new Rectangle(240, 390, 190, 30), "Chiara Boscariol", color, arial18));

            rows.Add(new MyButton(new Rectangle(0, 550, 480, 30), Strings.EMail, color, arial16));
            rows.Add(new MyButton(new Rectangle(0, 575, 480, 30), Strings.Adress, color, arial16));
            rows.Add(new MyButton(new Rectangle(0, 600, 480, 30), Strings.ThanksForPlaying, color, names));

            rows.Add(new MyButton(new Rectangle(116, 460, 84, 40), Strings.Version, color, arial16));
            rows.Add(new MyButton(new Rectangle(116, 500, 84, 40), (Game.IsTrial() ? Strings.TrialVersion : "1 . 0"), color, arial16));
            //rows.Add(new MyButton(new Rectangle(0, 630 + 800, 480, 100), Strings.Version + (Game.IsTrial() ? " - " + Strings.TrialVersion : ""), Color.Green, names, Color.Gold, emptyTexture));


            var box = Game.Content.Load<Texture2D>("Textures/bar_box");
            menu = new MyButton(new Rectangle(140, 740, 200, 50), Strings.ALevel_MainMenu, Color.White, titles, TextPos.Center, box, Color.White);

            transition_complete = false;
            base.LoadContent();
        }
        int transition_velocity = 100;
       
        public override void Update(GameTime gameTime)
        {
            if (rows[0].rectangle.Y <= 20)
                transition_complete = true;
            if (!transition_complete)
            {
                
                var deltay = -(int)(transition_velocity * (float)gameTime.ElapsedGameTime.TotalSeconds);
                //title.rectangle.Y += deltay;
                //email.rectangle.Y += deltay;
                menu.rectangle.Y += deltay;
                for (int i = 0; i < rows.Count; i++)
                    rows[i].rectangle.Y += deltay;
            }
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                StateManager.SelectMainMenu();
                return;
            } 
            base.Update(gameTime);
        }
        Rectangle bg_rect = new Rectangle(0, 0, 480, 800);
        Rectangle barRect = new Rectangle(0, 720, 480, 80);
        Color barColor = new Color(31, 31, 31);
        public override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);
            spriteBatch.Begin();
            spriteBatch.Draw(credits_bg, bg_rect, Color.White);
            for (int i = 0; i < rows.Count; i++)
                rows[i].Draw(spriteBatch);
            //email.Draw(spriteBatch);
            spriteBatch.Draw(whiteTexture, barRect, barColor);
            menu.Draw(spriteBatch);
            spriteBatch.End();
            base.Draw(gameTime);
        }
        Point tp = new Point();
        void on_tap(Vector2 tap_position)
        {
            if (!transition_complete) return;
            tp.X = (int)tap_position.X;
            tp.Y = (int)tap_position.Y;
            
            if (menu.tapped_on(tp))
                StateManager.SelectMainMenu();
        }
    
    }
}
